import { TalentManager } from "../core/Talent";
import { Util } from "../core/Util";

enum EDataKey {
    LONG_TIME = "LONG_TIME",
    AP = "AP",
    HP = "HP",
    DP = "DP",
    EXP = "EXP",
    GOLD= "GOLD",
    TALENT= "TALENT",
}

/**
 * 玩家数据
 */
export class PlayerData {
    loginTime: number = 0;
    //玩家属性
    ap: number = 5;
    dp: number = 2;
    hp: number = 100;
    exp: number = 0;
    gold: number = 0;

    talentMgr: TalentManager = new TalentManager();


    constructor() {
        
    }

    getPackage() {
        let data = {};
        data[EDataKey.LONG_TIME] = this.loginTime;
        data[EDataKey.AP] = this.ap;
        data[EDataKey.DP] = this.dp;
        data[EDataKey.HP] = this.hp;
        data[EDataKey.EXP] = this.exp;
        data[EDataKey.TALENT] = this.talentMgr.getPackage();
        data[EDataKey.GOLD] = this.gold;
        return data;
    }

    setPackage(data: {}) {
        if(!data) {
            return;
        }
        this.loginTime = Util.getSafeData<number>(data[EDataKey.LONG_TIME],0);
        this.ap = Util.getSafeData<number>(data[EDataKey.AP],5);
        this.dp = Util.getSafeData<number>(data[EDataKey.DP],2);
        this.hp = Util.getSafeData<number>(data[EDataKey.HP],100);
        this.exp =Util.getSafeData<number>(data[EDataKey.EXP],0);
        this.talentMgr.setPackage(Util.getSafeData<{}>(data[EDataKey.TALENT],{}));
        this.gold = Util.getSafeData<number>(data[EDataKey.GOLD],0);
    }
}
